package com.lmpop.trade.util;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.lmpop.trade.ts_area.Ts_areaInfo;
import com.kukugame.util.ipsearch.IPSeeker;

public class IpSearch
{
  Ts_areaInfo areaifo;

  public IpSearch()
  {
    this.areaifo = new Ts_areaInfo();
  }

  public String getIpAddr(HttpServletRequest request)
  {
    String ip = request.getHeader("x-forwarded-for");
    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
      ip = request.getRemoteAddr();
    }

    if (ip.equals("127.0.0.1")) {
      return "61.190.17.62";
    }
    return ip;
  }

  public String getIpName(String ipaddresss)
  {
    return IPSeeker.getInstance().getCountry(ipaddresss);
  }

  public String getAreaId(HttpServletRequest request)
  {
    String ipname = getIpName(getIpAddr(request));
    System.out.print("[ipname]==" + ipname);
    List arealist = this.areaifo.getSonAreaById("000000000000000");
    String resultAreaId = "";
    String areaid = ""; String areaname = "";
    if ((arealist != null) && (arealist.size() > 0)) {
      Map map = new Hashtable();
      for (int i = 0; i < arealist.size(); i++) {
        map = (Hashtable)arealist.get(i);
        if (map.get("area_id") != null) areaid = map.get("area_id").toString();
        if (map.get("area_name") != null) areaname = map.get("area_name").toString();
        if (areaname.indexOf(ipname) > -1) {
          System.out.print("[areaname]==" + areaname);
          String son_result_area_id = getSonIpAreaId(areaid, ipname);
          if (son_result_area_id.equals(""))
            resultAreaId = areaid;
          else {
            resultAreaId = son_result_area_id;
          }
        }
      }
    }
    return resultAreaId;
  }

  public String getIpNameString(HttpServletRequest request)
  {
    String ipname = getIpName(getIpAddr(request));

    ipname = ipname.substring(ipname.indexOf("ʡ") + 1, ipname.length() - 1);
    return ipname;
  }

  public String getSonIpAreaId(String up_area_id, String ipname)
  {
    String resultAreaId = "";
    List sonarealist = this.areaifo.getSonAreaById(up_area_id);
    Map upmap = new Hashtable();
    if ((sonarealist != null) && (sonarealist.size() > 0)) {
      for (int k = 0; k < sonarealist.size(); k++) {
        upmap = (Hashtable)sonarealist.get(k);
        String son_area_id = ""; String son_area_name = "";
        if (upmap.get("area_id") != null) son_area_id = upmap.get("area_id").toString();
        if (upmap.get("area_name") != null) son_area_name = upmap.get("area_name").toString();
        if (son_area_name.indexOf(ipname) > -1) {
          resultAreaId = son_area_id;
          break;
        }
      }
    }
    return resultAreaId;
  }
}